package com.mickey.util;

import com.alibaba.fastjson.JSON;

/**
 * @Desc:
 * @Author:czx
 * @CreateDate:2019/1/11 0011$ 14:10$
 */
public class StringUtil {

    public static  <T> String beanToString(T value) {
        if(value == null){
            return null;
        }
        Class<?> clazz = value.getClass();
        if(clazz == int.class || clazz == Integer.class){
            return "" + value;
        }else if(clazz == String.class ){
            return (String)value;
        }else if(clazz == long.class || clazz == Long.class){
            return "" + value;
        }else{
            return JSON.toJSONString(value);
        }
    }

    public static  <T> T stringToBean(String str,Class<T> clazz) {
        if(str == null|| str.length()<=0 || clazz == null){
            return null;
        }
        if(clazz == int.class || clazz == Integer.class){
            return (T) Integer.valueOf(str);
        }else if(clazz == String.class ){
            return (T)str;
        }else if(clazz == long.class || clazz == Long.class){
            return (T) Long.valueOf(str);
        }else{
            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }
}
